/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ACRestWebService;

import AjouConquerorBeans.Login;
import AjouConquerorBeans.LoginFacade;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

/**
 *
 * @author ajou
 */
@Stateless
@Path("ajouconquerorbeans.login")
public class LoginFacadeREST extends AbstractFacade<Login> {

    LoginFacade loginFacade = lookupLoginFacadeBean();
    @PersistenceContext(unitName = "AjouConquerorServer-warPU")
    private EntityManager em;

    public LoginFacadeREST() {
        super(Login.class);
    }

    @POST
    @Path("Logincheck")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String CheckId(@FormParam("uid") String id, @FormParam("pass") String pass) {
        String check = "fail";
        check = loginFacade.CheckCorrectIDPASS(id, pass);
        return check;
    }

    @POST
    @Path("account")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String AccountId(@FormParam("uid") String id, @FormParam("pass") String pass) {
        String check = "fail";
        check = loginFacade.AccountID(id, pass);
        return check;
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Login entity) {
        super.create(entity);
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Login entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") String id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Login find(@PathParam("id") String id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Login> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Login> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    private LoginFacade lookupLoginFacadeBean() {
        try {
            Context c = new InitialContext();
            return (LoginFacade) c.lookup("java:global/AjouConquerorServer/AjouConquerorServer-ejb/LoginFacade!AjouConquerorBeans.LoginFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
